package com.haonan.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.haonan.mapper.StudentMapper;
import com.haonan.pojo.dto.StudentDTO;
import com.haonan.pojo.dto.StudentQueryParam;
import com.haonan.pojo.entily.Student;
import com.haonan.pojo.vo.PageResult;
import com.haonan.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询学员信息
     * @param studentQueryParam
     * @return
     */
    public PageResult<Student> param(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        Page<Student> page= studentMapper.param(studentQueryParam);
        PageResult<Student> result = new PageResult<>(page.getTotal(), page.getResult());
        return result;
    }

    /**
     * 添加员工信息
     * @param studentDTO
     */
    public void insert(StudentDTO studentDTO) {
        LocalDateTime now = LocalDateTime.now();
        Student student=new Student();
        BeanUtils.copyProperties(studentDTO, student);
        student.setCreateTime(now);
        student.setUpdateTime(now);
        student.setViolationCount(Short.valueOf("0"));
        student.setViolationScore(Short.valueOf("0"));
        studentMapper.insert(student);
    }

    /**
     * 根据id批量删除学员
     * @param list
     */
    public void deleteById(List<Integer> list) {
        studentMapper.deleteById(list);
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    public Student findById(Integer id) {
        Student student = studentMapper.findById(id);
        return student;
    }

    /**
     * 编辑员工信息
     * @param studentDTO
     */
    public void update(StudentDTO studentDTO) {
        Student student=new Student();
        BeanUtils.copyProperties(studentDTO, student);
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    /**
     * 学员违纪情况
     * @param id
     * @param score
     */
    public void updateViolation(Integer id, Short score) {
        Student student = studentMapper.findById(id);
        Student student1=new Student();
        student1.setId(id);
        student1.setViolationCount((short) (student.getViolationCount()+1));
        student1.setViolationScore((short) (student.getViolationScore()+score));
        student1.setUpdateTime(LocalDateTime.now());

        studentMapper.update(student1);

    }
}
